#include <iostream>
#include <BinaryTree.hpp>
#include <stack>

using namespace std;

class BSTIterator
{
    // true:unused
    stack<TreeNode *> nodes;
    TreeNode *cur;

public:
    BSTIterator(TreeNode *root)
    {
        cur = root;
    }

    int next()
    {
        while (cur != nullptr)
        {
            nodes.push(cur);
            cur = cur->left;
        }
        cur = nodes.top();
        int val = cur->val;
        nodes.pop();
        cur = cur->right;
        return val;
    }

    bool hasNext()
    {
        return cur != nullptr || !nodes.empty();
    }
};

int main()
{
    cout << "Code173" << endl;
    TreeNode *root = CreatTree("[7, 3, 15, null, null, 9, 20]", "null");
    BSTIterator it(root);
    cout << boolalpha << it.hasNext() << endl;
    cout << it.next() << endl;
    cout << it.next() << endl;
    cout << it.next() << endl;
    cout << it.hasNext() << endl;
    cout << it.next() << endl;
    cout << it.next() << endl;
    cout << it.hasNext() << endl;
    return 0;
}